﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using System.Collections;
using ExpertMIS.Entities;
using System.Configuration;

namespace ExpertMIS.Workships
{
    public partial class UserProfile : ExpertMIS.Codes.PostPage
    {
        private ADMUserBroker _userBroker = new ADMUserBroker();

        private void LoadUserForm()
        {
            Hashtable htKeys = new Hashtable();
            htKeys.Add("OID", CurrentUser.OID);
            ADMUserEntity user = _userBroker.GetEntity(htKeys) as ADMUserEntity;

            txName.Text = user.Name;
            txPhone.Text = user.Tel;
            txEMail.Text = user.Email;

            imgGraphic.ImageUrl = "~/Service/GetPhoto.aspx?w=128&h=128&filename=" + user.Graphic;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadUserForm();
            }
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            _userBroker.ChangePassword(CurrentUser.OID, txPassWD.Text);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Hashtable htKeys = new Hashtable();
            htKeys.Add("OID", CurrentUser.OID);
            ADMUserEntity user = _userBroker.GetEntity(htKeys) as ADMUserEntity;

            user.Name = txName.Text;
            user.Tel = txPhone.Text;
            user.Email = txEMail.Text;

            ///////////////////////////////////////////////////////////
            // Expert Photo
            if (fuGraphic.HasFile)
            {
                string filename = Guid.NewGuid().ToString() + "." + System.IO.Path.GetExtension(fuGraphic.FileName);
                string filepath = Server.MapPath(ConfigurationManager.AppSettings["UserUpload"]);
                fuGraphic.SaveAs(filepath + filename);
                user.Graphic = filename;
            }

            _userBroker.SaveEntity(user, null);

            LoadUserForm();
        }
    }
}
